﻿using System;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using AzureTracer.Common;
using AzureTracer.Model;
using AzureTracer.Services;
using Microsoft.Phone.Shell;
using System.Security.Cryptography;
using System.Text;

namespace AzureTracer
{

    public class DataStateManager
    {
        protected  const string LOGSERVICEDATA = "LogServiceData";

        const string AccountName ="AccountName";
        const string AccountKey = "AccountKey";
        const string MaxDaysSync ="MaxDaysSync";
        const string ScheduleTimeInMinutes = "ScheduleTimeInMinutes";
        const string LastKeySync = "LastKeySync";
        const string LastTimeSync = "LastTimeSync";
        const string BackgroundItemsCount = "BackgroundItemsCount";
        const string AnySyncEnding = "AnySyncEnding";

        public static void SaveGlobalDataToStorage(ILogService LogService)
        {
            IsolatedStorageHelper.SaveList<LogEntity>(LOGSERVICEDATA + ".data", LogService.LogList);
        }

        public static void SaveGlobalSettings(IAppSettings AppSettings)
        {
            IsolatedStorageSettings.ApplicationSettings[LastKeySync] = AppSettings.LastKeySync;
            IsolatedStorageSettings.ApplicationSettings[BackgroundItemsCount] = AppSettings.BackgroundItemsCount;
            IsolatedStorageSettings.ApplicationSettings[LastTimeSync] = AppSettings.LastTimeSync;

            IsolatedStorageSettings.ApplicationSettings[AccountName] = ProtectedData.Protect(Encoding.UTF8.GetBytes( AppSettings.AccountName),null);
            IsolatedStorageSettings.ApplicationSettings[AccountKey] = ProtectedData.Protect(Encoding.UTF8.GetBytes(AppSettings.AccountKey), null);
            IsolatedStorageSettings.ApplicationSettings[MaxDaysSync] = AppSettings.MaxDaysSync;
            IsolatedStorageSettings.ApplicationSettings[ScheduleTimeInMinutes] = AppSettings.ScheduleTimeInMinutes;
            IsolatedStorageSettings.ApplicationSettings[AnySyncEnding] = AppSettings.AnySyncEnding;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        public static void LoadGlobalDataFromStorage(ILogService LogService)
        {
            LogService.LogList = IsolatedStorageHelper.LoadList<LogEntity>(LOGSERVICEDATA + ".data");
        }

        public static void LoadGlobalSettings(IAppSettings AppSettings)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LastKeySync))
                AppSettings.LastKeySync = (string)IsolatedStorageSettings.ApplicationSettings[LastKeySync];
            if (IsolatedStorageSettings.ApplicationSettings.Contains(BackgroundItemsCount))
                AppSettings.BackgroundItemsCount = (int)IsolatedStorageSettings.ApplicationSettings[BackgroundItemsCount];
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LastTimeSync))
                AppSettings.LastTimeSync = (DateTime)IsolatedStorageSettings.ApplicationSettings[LastTimeSync];

            if (IsolatedStorageSettings.ApplicationSettings.Contains(AccountName))
            {
                byte[] accountEncripted = ProtectedData.Unprotect((byte[])IsolatedStorageSettings.ApplicationSettings[AccountName], null);
                AppSettings.AccountName = Encoding.UTF8.GetString(accountEncripted, 0, accountEncripted.Length);
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(AccountKey))
            {
                byte[] keyEncripted = ProtectedData.Unprotect((byte[])IsolatedStorageSettings.ApplicationSettings[AccountKey], null);
                AppSettings.AccountKey = Encoding.UTF8.GetString(keyEncripted, 0, keyEncripted.Length);                
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(MaxDaysSync))
                AppSettings.MaxDaysSync = (int)IsolatedStorageSettings.ApplicationSettings[MaxDaysSync];
            if (IsolatedStorageSettings.ApplicationSettings.Contains(ScheduleTimeInMinutes))
                AppSettings.ScheduleTimeInMinutes = (int)IsolatedStorageSettings.ApplicationSettings[ScheduleTimeInMinutes];
            if (IsolatedStorageSettings.ApplicationSettings.Contains(AnySyncEnding))
                AppSettings.AnySyncEnding = (bool)IsolatedStorageSettings.ApplicationSettings[AnySyncEnding];
        }        
    }
}
